#include <iostream>
#include <cassert>
#include <vector>
#include "sqlite.hpp"

int select_stu_callback(void* arg, int col_count, char** result, char** fields_name)
{
    std::vector<std::string>* array = static_cast<std::vector<std::string>*>(arg);
    static int count = 0;
    if(count++ == 0)
    {
        array->push_back(*fields_name);
    }
    array->push_back(result[0]);
    return 0;
}

int main()
{
    SqliteHelper helper("./testdb");

    // 1.创建或打开库文件
    assert(helper.open());
    // 2.创建表（若不存在），学生信息：学号、姓名、年龄
    const char* create_sql = "create table if not exists student(sn int primary key, name varchar(24), age int)";
    assert(helper.exec(create_sql, nullptr, nullptr));
    // 3.对数据：新增、修改、删除、查询
    // 新增insert
    const char* insert_sql = "insert into student values(1, '小明', 18), (2, '老六', 20), (3, '老八', 19)";
    assert(helper.exec(insert_sql, nullptr, nullptr));
    // 修改update
    const char* update_sql = "update student set age=22 where name='小明'";
    assert(helper.exec(update_sql, nullptr, nullptr));
    // 删除delete
    const char* delete_sql = "delete from student where name='小明'";
    assert(helper.exec(delete_sql, nullptr, nullptr));
    // 查询select
    const char* select_sql = "select name from student";
    std::vector<std::string> v_msg;
    assert(helper.exec(select_sql, select_stu_callback, &v_msg));
    for(const auto& msg: v_msg)
    {
        std::cout << msg << std::endl;
    }

    return 0;
}
